package com.supermap.dataservice.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.user.dao.UserTypeDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.user.UserTypeService;
import com.supermap.dubbo.api.user.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lxs
 * @Title:服务对象 逻辑层
 * @Description:
 * @date 2018/7/26  16:49
 */
@Service
public class UserTypeServiceImpl implements UserTypeService {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(UserTypeServiceImpl.class);
    @Resource
    private UserTypeDao userTypeDao;

    @Override
    public JSONUtil getRoleList() {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Role> list = userTypeDao.getRoleList();
            jsonUtil.setMsg("Get getRoleList success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(list);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getRoleList=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get getRoleList flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getUserTypeList(String areaCode, String code) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            long t1 = System.currentTimeMillis();
            List<UserType> list = userTypeDao.getUserTypeList(areaCode, code);
            jsonUtil.setMsg("Get getUserTypesList success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(list);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserTypesList=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get getUserTypesList flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getVipById(String id) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Vip> vip = userTypeDao.getVipById(id);
            jsonUtil.setMsg("Get getVipByuserid success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(vip);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserTypesList=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get getVipByuserid flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getUserById(String id) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            User user = userTypeDao.getUserById(id);
            jsonUtil.setMsg("Get getVipByuserid success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(user);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserTypesList=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get getVipByuserid flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateEpathByUserId(String epath, String username) {
        //初始化Json封装工具类
        InterNetInfo info = userTypeDao.getUserId(username);

        JSONUtil jsonUtil = new JSONUtil();
        if (info.getRolecode().equals("4")) {

            info.setQyyhUrl(epath);
            int count = userTypeDao.getCompanyNum(info.getId());
            if (count > 0) {
                userTypeDao.updateQYYHbyUserId(info);
            } else {
                int num = userTypeDao.insertQYYH(info);
                resultJson(num > 0);
            }
        } else if (info.getRolecode().equals("10")) {
            info.setZmxcUrl(epath);
            int count = userTypeDao.getCountrysideNum(info.getId());
            if (count > 0) {
                userTypeDao.updateZMXCbyUserId(info);
            } else {
                int num = userTypeDao.insertZMXC(info);
                resultJson(num > 0);
            }
        } else if (info.getRolecode().equals("3")) {
            info.setZjUrl(epath);
            int count = userTypeDao.getExpertNum(info.getId());
            if (count > 0) {
                userTypeDao.updateEXPERTbyUserId(info);
            } else {
                int num = userTypeDao.insertEXPERT(info);
                resultJson(num > 0);
            }
        } else if (info.getRolecode().equals("2")) {
            info.setZzdhUrl(epath);
            int count = userTypeDao.getGrowerNum(info.getId());
            if (count > 0) {
                userTypeDao.updateZZDHbyUserId(info);
            } else {
                int num = userTypeDao.insertZZDH(info);
                resultJson(num > 0);
            }
        } else if (info.getRolecode().equals("5")) {

        } else if (info.getRolecode().equals("6")) {
            info.setHzsUrl(epath);
            int count = userTypeDao.getCooperativeNum(info.getId());
            if (count > 0) {
                userTypeDao.updateHZSYHbyUserId(info);
            } else {
                int num = userTypeDao.insertHZSYH(info);
                resultJson(num > 0);
            }
        } else if (info.getRolecode().equals("7")) {
            info.setGxsUrl(epath);
            int count = userTypeDao.getSuplyNum(info.getId());
            if (count > 0) {
                userTypeDao.updateGXSbyUserId(info);
            } else {
                int num = userTypeDao.insertGXS(info);
                resultJson(num > 0);
            }
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getEXPERTListByareaCode(Table table, String areaCode, String subjectId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Map<String, Object>> userList = userTypeDao.getEXPERTListByareaCode(table, areaCode, subjectId);
            int count = userTypeDao.getEXPERTCount(areaCode, subjectId);
            table.setCount(count);
            table.setData(userList);
            jsonUtil.setMsg("getUserListByareaCode  success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserListByareaCode=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("getUserListByareaCode flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getInterNetInfo(int userid, String roleCode) {
        JSONUtil jsonUtil = new JSONUtil();
        if (roleCode != null && !"".equals(roleCode) && userid > 0) {

            List<InterNetInfo> list = userTypeDao.getInterInfo(userid, roleCode);
            if (!list.isEmpty() && list != null) {
                jsonUtil.setMsg("get Success!");
                jsonUtil.setData(list);
                jsonUtil.setCode(200);
                jsonUtil.setIsSuccess(true);
            } else {
                jsonUtil.setMsg("get Fail!");
                jsonUtil.setCode(200);
                jsonUtil.setIsSuccess(true);
            }
        } else {
            jsonUtil.setMsg("userid或者rolecode为空！");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getTsubjects() {
        JSONUtil jsonUtil = new JSONUtil();
        jsonUtil.setMsg("get Success!");
        jsonUtil.setData(userTypeDao.getTsubjects());
        jsonUtil.setCode(200);
        jsonUtil.setIsSuccess(true);
        return jsonUtil;
    }

    @Override
    public JSONUtil updateUserRole(InterNetInfo interNetInfo) {
        JSONUtil jsonUtil = new JSONUtil();
        interNetInfo.setDel_flg(0);

        InterNetInfo info = new InterNetInfo();
        info.setId(interNetInfo.getId());
        info.setDel_flg(1);

        if (interNetInfo != null) {
            TUserRole tUserRole = userTypeDao.getUserRoleId(interNetInfo.getId());
            if (tUserRole.getRoleId() == 2) {
                userTypeDao.updateZZDHbyUserId(info);
            } else if (tUserRole.getRoleId() == 3) {
                userTypeDao.updateEXPERTbyUserId(info);
            } else if (tUserRole.getRoleId() == 4) {
                userTypeDao.updateQYYHbyUserId(info);
            } else if (tUserRole.getRoleId() == 6) {
                userTypeDao.updateHZSYHbyUserId(info);
            } else if (tUserRole.getRoleId() == 7) {
                userTypeDao.updateGXSbyUserId(info);
            } else if (tUserRole.getRoleId() == 10) {
                userTypeDao.updateZMXCbyUserId(info);
            }
            int num = userTypeDao.updatePTYHbyId(interNetInfo);
            if (num > 0) {
                if (!interNetInfo.getRolecode().equals(tUserRole.getRoleId())) {

                    if (interNetInfo.getRolecode().equals("1")) {
                        userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                    } else if (interNetInfo.getRolecode().equals("2")) {
                        int count = userTypeDao.getGrowerNum(interNetInfo.getId());
                        if (count > 0) {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.updateZZDHbyUserId(interNetInfo);
//                            int numRole = userTypeDao.insertZZDH(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.insertZZDH(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        }
                    } else if (interNetInfo.getRolecode().equals("3")) {
                        userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                        int count = userTypeDao.getExpertNum(interNetInfo.getId());
                        if (count > 0) {
                            int id = userTypeDao.getExpId(interNetInfo.getId());
                           userTypeDao.updateEXPERTbyUserId(interNetInfo);
                            if (id > 0) {
                                int exId = userTypeDao.getExId(id);
                                if (exId > 0) {
                                    int exSubNum =userTypeDao.updateExSub(Integer.parseInt(interNetInfo.getSuejectid()),id);
                                    jsonUtil = resultJson(exSubNum > 0);
                                }else{
                                    int exSubNum = userTypeDao.insertExSub(id, Integer.parseInt(interNetInfo.getSuejectid()));
                                       jsonUtil = resultJson(exSubNum > 0);
                                }

//                            int numRole = userTypeDao.insertEXPERT(interNetInfo);
//                                    if (numRole > 0) {
//                                    if (exId == 0) {
//                                        int exSubNum = userTypeDao.insertExSub(id, Integer.parseInt(interNetInfo.getSuejectid()));
//                                        jsonUtil = resultJson(exSubNum > 0);
//                                    } else {
//                                        jsonUtil = resultJson(numRole > 0);
//                                    }

                            }

                        } else {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.insertEXPERT(interNetInfo);
                            if (numRole > 0) {
                                int id = userTypeDao.getExpId(interNetInfo.getId());
                                if (id > 0) {
                                    int exSubNum = userTypeDao.insertExSub(id, Integer.parseInt(interNetInfo.getSuejectid()));
                                    jsonUtil = resultJson(exSubNum > 0);
                                }
                            }
                        }

                    } else if (interNetInfo.getRolecode().equals("4")) {
                        int count = userTypeDao.getCompanyNum(interNetInfo.getId());
                        if (count > 0) {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.updateQYYHbyUserId(interNetInfo);
//                            int numRole = userTypeDao.insertQYYH(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.insertQYYH(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        }

                    } else if (interNetInfo.getRolecode().equals("6")) {
                        int count = userTypeDao.getCooperativeNum(interNetInfo.getId());
                        if (count > 0) {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.updateHZSYHbyUserId(interNetInfo);
//                            int numRole = userTypeDao.insertHZSYH(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.insertHZSYH(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        }
                    } else if (interNetInfo.getRolecode().equals("7")) {
                        int count = userTypeDao.getSuplyNum(interNetInfo.getId());
                        if (count > 0) {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.updateGXSbyUserId(interNetInfo);
//                            int numRole = userTypeDao.insertGXS(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.insertGXS(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        }

                    } else if (interNetInfo.getRolecode().equals("10")) {
                        int count = userTypeDao.getCountrysideNum(interNetInfo.getId());
                        if (count > 0) {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.updateZMXCbyUserId(interNetInfo);
//                            int numRole = userTypeDao.insertZMXC(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                            int numRole = userTypeDao.insertZMXC(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        }
                    } else {
                        if (interNetInfo.getRolecode().equals("1")) {
                            userTypeDao.updateUserRole(String.valueOf(interNetInfo.getId()), interNetInfo.getRolecode());
                        } else if (interNetInfo.getRolecode().equals("2")) {
                            int numRole = userTypeDao.updateZZDHbyUserId(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else if (interNetInfo.getRolecode().equals("3")) {
                            int numRole = userTypeDao.updateEXPERTbyUserId(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else if (interNetInfo.getRolecode().equals("4")) {
                            int numRole = userTypeDao.updateQYYHbyUserId(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else if (interNetInfo.getRolecode().equals("6")) {
                            int numRole = userTypeDao.updateHZSYHbyUserId(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else if (interNetInfo.getRolecode().equals("7")) {
                            int numRole = userTypeDao.updateGXSbyUserId(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        } else if (interNetInfo.getRolecode().equals("10")) {
                            int numRole = userTypeDao.updateZMXCbyUserId(interNetInfo);
                            jsonUtil = resultJson(numRole > 0);
                        }
                    }
                } else {
                    resultJson(false);
                }
            }
        }
        return jsonUtil;
    }

    /**
     * 返回json
     **/
    public JSONUtil resultJson(Boolean b) {

        JSONUtil jsonUtil = new JSONUtil();
        if (b) {
            jsonUtil.setMsg("更新成功!");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        } else {
            jsonUtil.setMsg("更新失败!");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        }
        return jsonUtil;
    }
}
